// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class mapControl extends cc.Component {

    //  获取瓦片图资源
    map: cc.TiledMap;
    player: cc.Node = null;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        this.map = this.getComponent(cc.TiledMap);
        // console.log(this.map);
        // 获取指定的对象层
        let playerObj = this.map.getObjectGroup('object1');
        // 获取对象数组
        // let playerObjArr = playerObj.getObjects();
        // 获取对象
        let playerStartPos = playerObj.node.getChildByName("startPos"), playerEndPos = playerObj.node.getChildByName("endPos");

        // 在开始位置创建玩家对象
        // 加载资源
        cc.resources.load('prefab/tilemap/player', cc.Prefab,  (err, playerPre)=> { 
            // 实例化预设体
            this.player = cc.instantiate(playerPre);
            
            this.player.setParent(this.node.getChildByName('object1'));
            
            // // // 设置玩家位置
            this.player.x= playerStartPos.x;
            this.player.y = playerStartPos.y;
            
            console.log(this.player);
            
        })
    }

    update (dt) {
        // 摄像头跟随玩家
        if (this.player != null) {
            cc.Camera.main.node.x = this.player.x;
            cc.Camera.main.node.y = this.player.y;
        }
    }
}
